import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.*;
import java.net.Socket;
import java.util.*;

import org.junit.Test;
import pojo.Group;
import pojo.Message;
import pojo.User;

/**
 * @author Administrator
 */
public class MainFunction {

    public static String groupNameBefore ="666";

    private static ArrayList<ServerThread> stList = new ArrayList<ServerThread>();
    //在线用户集合

    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws IOException {

    }
    //测试方法

    public static boolean checkReg(String name, String account, String password) {
        return true;
    }
    //注册验证

    public static boolean checkLogin(User user) {
        UserMapper userMapper = loadUserMapperXml();
        List<User> users = userMapper.checkLogin(user.getAccount(), user.getPassword());
        if (!users.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
    //登录验证

    public static UserMapper loadUserMapperXml() {
        String resource = "resources/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //设置事务自动提交
        UserMapper usermapper = sqlSession.getMapper(UserMapper.class);
        return usermapper;
    }

    public static CompanionMapper loadCompanionMapperXml() {
        String resource = "resources/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //设置事务自动提交
        CompanionMapper companionMapper = sqlSession.getMapper(CompanionMapper.class);
        return companionMapper;
    }

    public static ChatMessageMapper loadChatMessageMapper() {
        String resource = "resources/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //设置事务自动提交
        ChatMessageMapper chatMessageMapper = sqlSession.getMapper(ChatMessageMapper.class);
        return chatMessageMapper;
    }

    public static NewFriendsMapper loadNewFriendsMapper() {
        String resource = "resources/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //设置事务自动提交
        NewFriendsMapper newFriendsMapper = sqlSession.getMapper(NewFriendsMapper.class);
        return newFriendsMapper;
    }

    public static GroupMessageMapper loadGroupMessageMapper() {
        String resource = "resources/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //设置事务自动提交
        GroupMessageMapper groupMessageMapper = sqlSession.getMapper(GroupMessageMapper.class);
        return groupMessageMapper;
    }

    public static GroupMapper loadGroupMapper() {
        String resource = "resources/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //设置事务自动提交
        GroupMapper groupMapper = sqlSession.getMapper(GroupMapper.class);
        return groupMapper;
    }


    public static User saveInformation(User user) {
        UserMapper userMapper = loadUserMapperXml();
        userMapper.saveUser(user);
        return user;
    }

    public static void updateUser() {
        UserMapper userMapper = loadUserMapperXml();

    }
    //更新用户信息

    public static void deleteUser() {

    }
    //踢出(删除)用户

    public static void parseImage(Socket s, User user) {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\photo\\" + user.getAccount() + ".jpg"));
            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            byte[] b = new byte[2048];
            int len = -1;
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            bis.close();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String returnImage(String account) throws IOException {
        String str = ImgUtil.encodeImage("src\\photo\\" + account + ".jpg");
        return str;
    }


    public static List<User> newFriendsMessage(String account) {
        NewFriendsMapper newFriendsMapper = loadNewFriendsMapper();
        UserMapper userMapper = loadUserMapperXml();
        List<String> selectmessage = newFriendsMapper.selectmessage(account);
        List<User> users = new ArrayList<>();
        for (String s : selectmessage) {
            User user = userMapper.selevtUserbyAccount(s);
            users.add(user);
        }
        return users;
    }

    public static void addNewFriend(String mAccount, String yAccount) {
        NewFriendsMapper newFriendsMapper = loadNewFriendsMapper();
        int i = newFriendsMapper.addFriend(mAccount, yAccount);
        System.out.println(i);
    }

    public static void deleteMessage(String mAccount, String yAccount) {
        NewFriendsMapper newFriendsMapper = loadNewFriendsMapper();
        newFriendsMapper.delectAckMessage(mAccount, yAccount);
    }

    public static void addFriend(String mAccount, String yAccount) {
        CompanionMapper companionMapper = loadCompanionMapperXml();
        companionMapper.saveFrined(mAccount, yAccount);
    }

    public static void saveMessage(String aAccount, String bAccount, String message, String data) {
        ChatMessageMapper chatMessageMapper = loadChatMessageMapper();
        chatMessageMapper.saveMessage(aAccount, bAccount, message, data);
    }

    public static ArrayList<Message> getMessage(String aAccount, String bAccount) {
        ChatMessageMapper chatMessageMapper = loadChatMessageMapper();
        ArrayList<Message> messages = chatMessageMapper.getMessage(aAccount, bAccount);
        return messages;
    }

    public static ArrayList<Message> getAllMessage(String account) {
        ChatMessageMapper chatMessageMapper = loadChatMessageMapper();
        ArrayList<Message> accountMessages = chatMessageMapper.getAllMessage(account);
        return accountMessages;
    }

    public static void deleteFriend(String account) {
        CompanionMapper companionMapper = loadCompanionMapperXml();
        ChatMessageMapper chatMessageMapper = loadChatMessageMapper();
        chatMessageMapper.deleteMessage(account);
        companionMapper.deleteFriend(account);
    }

    public static ArrayList<String> getMembers(String groupAccount) {
        GroupMapper groupMapper = loadGroupMapper();
        ArrayList<String> members = groupMapper.getMembers(groupAccount);
        return members;
    }

    public static ArrayList<String> getGroups(String account) {
        GroupMapper groupMapper = loadGroupMapper();
        ArrayList<Integer> gs = groupMapper.getGroups(account);
        ArrayList<String> groups = new ArrayList<>();
        for (Integer i : gs) {
            groups.add(i+"");
        }
        return groups;
    }
    public static String bulidGroup(String groupName ,String hostAccount){
        GroupMapper groupMapper = loadGroupMapper();
        Random random = new Random();
        int number = random.nextInt(900000)+100000;
        String groupAccount = groupNameBefore+number;
        groupMapper.bulidGroup(groupAccount,groupName,hostAccount,hostAccount);

        return groupAccount;
    }

    public static ArrayList<Group> writeGroups(User user){
        GroupMapper groupMapper = loadGroupMapper();
        UserMapper userMapper = loadUserMapperXml();
        ArrayList<String> groups = getGroups(user.getAccount());
        ArrayList<Group> groups1  = new ArrayList<>();
        for(String s :groups){
            Group g = new Group();
            g.setGroupAccount(s);
            g.setGroupName(groupMapper.getGroupName(s).get(0));
            ArrayList<String> members = groupMapper.getMembers(s);
            ArrayList<User> users = new ArrayList<>();
            for(String str : members){
                User user2 = userMapper.selevtUserbyAccount(str);
                users.add(user2);
            }
            g.setMembers(users);
            g.setHostAccount(groupMapper.getHostAccount(s).get(0));
            groups1.add(g);
        }
        return groups1;
    }
    public static void saveGroupMessage(String account,String groupAccount,String message,String date){
        GroupMessageMapper groupMessageMapper = loadGroupMessageMapper();
        groupMessageMapper.saveMessage(account,groupAccount,message,date);
    }
    public static ArrayList<String> getGroupAccount(String account){
        GroupMapper groupMapper = loadGroupMapper();
        ArrayList<String> groupAccounts = groupMapper.getGroupAccounts(account);
        return groupAccounts;
    }
    public static ArrayList<Message> getGroupMessage(String account){
        ArrayList<String> groupAccount = getGroupAccount(account);
        GroupMessageMapper groupMessageMapper = loadGroupMessageMapper();
        ArrayList<Message> messages = new ArrayList<>();
        for(String s : groupAccount){
            messages.addAll(groupMessageMapper.getMessage(s));
        }
        return messages;
    }
    public static void saveGroup(String groupAccount,String memberaccount) {
        GroupMapper groupMapper = loadGroupMapper();
        ArrayList<String> groupNames = groupMapper.getGroupName(groupAccount);
        ArrayList<String> hostAccount = groupMapper.getHostAccount(groupAccount);
        if (groupNames == null || hostAccount == null) {

        } else {
            groupMapper.agreeAply(groupAccount, groupNames.get(0), hostAccount.get(0), memberaccount);
        }
    }

    public static void hostExit(String hostAccount){
        GroupMapper groupMapper = loadGroupMapper();
        GroupMessageMapper groupMessageMapper = loadGroupMessageMapper();
        groupMapper.hostExit(hostAccount);
        groupMessageMapper.getMessage(hostAccount);
    }

    public static void memberExit(String memberAccount){
        GroupMapper groupMapper = loadGroupMapper();
        groupMapper.memeberExit(memberAccount);
    }

    public static boolean judgeExist(String senderAccount,String receiverAccount){
        NewFriendsMapper newFriendsMapper = loadNewFriendsMapper();
        String select = newFriendsMapper.select(senderAccount, receiverAccount);
        if(select==null)
        {
            return false;
        }
        else {
            return true;
        }
    }

}
